//
//  XFPictureSelectController.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/25.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFPictureSelectController.h"
#import "XFPictureViewCell.h"

#define kPictureViwCellMargin 10
#define kMaxPicture 3

@interface XFPictureSelectController () <XFPictureViewCellDelegate,
                                         UIImagePickerControllerDelegate,
                                         UINavigationControllerDelegate>



@end

@implementation XFPictureSelectController

- (NSMutableArray *)picArray {

  if (_picArray == nil) {
    _picArray = [NSMutableArray array];
  }
  return _picArray;
}

static NSString *const reuseIdentifier = @"Cell";

- (instancetype)init {

  UICollectionViewFlowLayout *layout =
      [[UICollectionViewFlowLayout alloc] init];
  NSInteger colume = 4;
  CGFloat cellW = (kScreenW - kPictureViwCellMargin * (colume + 1)) / colume;
  layout.itemSize = CGSizeMake(cellW, cellW);
  layout.minimumLineSpacing = kPictureViwCellMargin;
  layout.minimumInteritemSpacing = kPictureViwCellMargin;
  layout.sectionInset =
      UIEdgeInsetsMake(kPictureViwCellMargin, 0, kPictureViwCellMargin, 0);

  return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
  [super viewDidLoad];

  [self.collectionView registerClass:[XFPictureViewCell class]
          forCellWithReuseIdentifier:reuseIdentifier];
}

#pragma mark <UIImagePickerControllerDelegate>

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary<NSString *, id> *)editingInfo {

  [self.picArray addObject:image];
  [self.collectionView reloadData];

  //绑定协议之后, dismiss 交给程序员
  [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark <XFPictureViewCellDelegate>

- (void)addPictureWithpictureViewCell:(XFPictureViewCell *)pictureViewCell {

  if (pictureViewCell.picture != nil) {
    return;
  }

  UIImagePickerController *pickVc = [[UIImagePickerController alloc] init];

  pickVc.delegate = self;

  [self presentViewController:pickVc animated:YES completion:nil];
}

- (void)deletePictureWithpictureViewCell:(XFPictureViewCell *)pictureViewCell {

  NSIndexPath *indexPath =
      [self.collectionView indexPathForCell:pictureViewCell];

  [self.picArray removeObjectAtIndex:indexPath.item];

  [UIView animateWithDuration:0.5
      animations:^{
        pictureViewCell.alpha = 0;
      }
      completion:^(BOOL finished) {
        pictureViewCell.alpha = 1;
        [self.collectionView reloadData];
      }];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
  NSInteger count = self.picArray.count == kMaxPicture ? 0 : 1;
  return self.picArray.count + count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  XFPictureViewCell *cell =
      [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
                                                forIndexPath:indexPath];

  cell.delegate = self;

  if (self.picArray.count == indexPath.item) {
    cell.picture = nil;
  } else {
    cell.picture = self.picArray[indexPath.item];
  }

  return cell;
}

#pragma mark <UICollectionViewDelegate>

@end
